package com.ebook.ui.fragment.home.everyrecommend;

import com.ebook.database.dao.BookDao;
import com.ebook.entity.Book;
import com.ebook.ui.activity.BookDetailActivity;
import com.ebook.ui.sync.WorkSyncer;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BookSummary extends JPanel {
    private ImgContainer book_img;
    private BookLabel book_label;
    private Book book;
    public BookSummary(Book book){
        super();
        setLayout(null);
        setBackground(Color.white);
        this.book = book;
        this.setSize(154,200);
        book_img = new ImgContainer(book.getImage(),this);
        this.add(book_img);
        book_label = new BookLabel(book.getBook_name()+" "+book.getAuthors());
        this.add(book_label);
        MouseAdapter ma =
            new MouseAdapter(){
                @Override
                public void mouseClicked(MouseEvent e){
                    try {
                        notifyBookDetails();
                    } catch (NoSuchFieldException | IllegalAccessException ex) {
                        ex.printStackTrace();
                    }
                }
        };
        this.addMouseListener(ma);
        this.book_label.addMouseListener(ma);
    }
    public void notifyBookDetails() throws NoSuchFieldException, IllegalAccessException {
        WorkSyncer.webView.pushActivity(new BookDetailActivity(book));
    }

    public static void main(String[] args) {
        var xp = new ClassPathXmlApplicationContext("beans.xml");
        JFrame f = new JFrame();
        f.setBounds(300,200,1000,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(null);
        BookDao bd = xp.getBean("bookDao",BookDao.class);
        BookSummary bs = new BookSummary(bd.selectBookById(2));
        bs.setLocation(new Point(0,0));
        f.add(bs);
        BookSummary bs2 = new BookSummary(bd.selectBookById(3));
        bs2.setLocation(new Point(154,0));
        f.add(bs2);
        BookSummary bs3 = new BookSummary(bd.selectBookById(4));
        bs3.setLocation(new Point(308,0));
        f.add(bs3);
        f.setVisible(true);
    }
}
